'''
This is an example script for using itol.py
To use itol.py, you essentially have to instantiate the object, set the tree, submit the file to itol, then use the returned data
'''

import Itol
import Dataset
import ItolExport
import sys

print 'Running example itol and itolexport script'
print ''
print 'Creating the upload params'
#Create the dataset
dataset_location = 'example_data'
dataset_dict = dict({})
dataset_dict['Label'] = 'asdf'
dataset_dict['Separator'] = 'comma'
dataset_dict['Type'] = 'multibar'
test_dataset = Dataset.Dataset()
test_dataset.set_dataset_location(dataset_location)
test_dataset.add_param_dict(dataset_dict)


#Create the Itol class
test = Itol.Itol()

#Set the tree file
tree = 'example_tree'
test.set_tree_location(tree)
#Add parameters
itol_dict = dict({})
itol_dict['treeName'] = 'asdf'
itol_dict['treeFormat'] = 'newick'
test.add_upload_param_dict(itol_dict)
#Add the dataset
test.add_dataset(test_dataset)

#Submit the tree
print 'Uploading the tree.  This may take some time depending on how large the tree is and how much load there is on the itol server'
good_upload = test.upload_tree()
if good_upload == False:
    print 'There was an error:'+test.upload_output
    sys.exit(1)

#Read the tree ID
print 'Tree ID: '+str(test.tree_id)

#Read the iTOL API return statement
print 'iTOL output: '+str(test.upload_output)

#Website to be redirected to iTOL tree
print 'Tree Web Page URL: '+test.get_webpage()



#Export to pdf
print 'Exporting to pdf'
itol_exporter = test.get_itol_export()
export_location = 'example_pdf.pdf'
#itol_exporter = itolexport.ItolExport()
#itol_exporter.set_export_param_value('tree','18793532031912684633930')
itol_exporter.set_export_param_value('format', 'pdf')
itol_exporter.set_export_param_value('datasetList','dataset1')
itol_exporter.export('example_pdf.pdf')
print 'exported tree to ',export_location
